import pymongo

myclient = pymongo.MongoClient("mongodb://82.157.127.245:27999/")
#myclient = pymongo.MongoClient("mongodb://127.0.0.1:27999/")
mydb = myclient["weibo"]
mycollection = mydb["index"]


def insert_into_index(item, index):
    mycollection.insert_one({"item":item, "index": index})

def save_doc_num(num):
    myquery = {"num": "doc_num"}
    mydoc = mycollection.find_one(myquery)
    if mydoc is None:
        mycollection.insert_one({"num": "doc_num", "num_value": num})
    else:
        mycollection.delete_one(myquery)
        mycollection.insert_one({"num": "doc_num", "num_value": num})
def get_doc_num():
    myquery = {"num": "doc_num"}
    mydoc = mycollection.find_one(myquery)
    if mydoc is None:
        return 0
    return mydoc.get("num_value")

def find_by_item(item):
    myquery = {"item": item}
    mydoc = mycollection.find_one(myquery)
    return mydoc

def update_by_item(item, index):
    myquery = {"item": item}
    mycollection.delete_one(myquery)
    insert_into_index(item, index)

if __name__ == "__main__":
    #find_by_item("祝贺")
    print(get_doc_num())
    # save_doc_num(50)
    # save_doc_num(100)
    # save_doc_num(150)
    # save_doc_num(200)
    # print(get_doc_num())